<template>
  <Teleport to="#lanhu-plugin-container">
    <Transition name="toast">
      <div v-if="visible" class="toast-container">
        {{ message }}
      </div>
    </Transition>
  </Teleport>
</template>

<script setup lang="ts">
import { ref } from 'vue'

interface ToastProps {
  message?: string
  duration?: number
}

const props = withDefaults(defineProps<ToastProps>(), {
  message: '操作成功',
  duration: 2000,
})

const visible = ref(false)

defineExpose({
  show: () => {
    visible.value = true
    setTimeout(() => {
      visible.value = false
    }, props.duration)
  },
  hide: () => {
    visible.value = false
  },
})
</script>

<style scoped>
.toast-container {
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 999999;
  padding: 8px 16px;
  background-color: rgba(0, 0, 0, 0.8);
  color: #fff;
  text-align: center;
  border-radius: 4px;
  font-size: 14px;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.toast-enter-active,
.toast-leave-active {
  transition: all 0.3s ease;
}

.toast-enter-from {
  opacity: 0;
  transform: translateX(-50%) translateY(-10px);
}

.toast-leave-to {
  opacity: 0;
  transform: translateX(-50%) translateY(-10px);
}
</style>
